package boogui.movie;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ImageIcon;
import javax.swing.BoxLayout;
import javax.swing.JLabel;

import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;

public class S_MovieSeatDialog extends JDialog {

	private BtnActionListener al = null;	
	private MovieSeatDAO bd = new MovieSeatDAO();	

	private JCheckBox seats[][];
	private JLabel label[];

	private char isSelec;	

	private static String updateSeatSQL;

	private final JPanel contentPanel = new JPanel();
	
	public int as = 0;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			S_MovieSeatDialog dialog = new S_MovieSeatDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public S_MovieSeatDialog() {
		al = new BtnActionListener();
		setBounds(100, 100, 500, 500);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JPanel panel = new JPanel();
			contentPanel.add(panel, BorderLayout.NORTH);
			panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
			{
				JLabel lblNewLabel = new JLabel("S C R E E N");
				lblNewLabel.setFont(new Font("맑은 고딕", Font.BOLD, 24));
				panel.add(lblNewLabel);				
			}			
			{
				JPanel panel2 = new JPanel();
				contentPanel.add(panel2, BorderLayout.WEST);				
				panel2.setLayout(new GridLayout(13, 0, 5, 0));
				
				label = new JLabel[13];
				label[0] = new JLabel();
				panel2.add(label[0]);
				label[0].setIcon(new ImageIcon(S_MovieSeatDialog.class.getResource("/images/blank.png")));
				
				//왼쪽 영어 이미지
				for (int i = 1; i <= 12; ++i) {
					label[i] = new JLabel();
					panel2.add(label[i]);
					label[i].setIcon(new ImageIcon(S_MovieSeatDialog.class.getResource("/images/seat/seatab/" + i + ".png")));
				}

			}
			{
				JPanel panel3 = new JPanel();
				contentPanel.add(panel3, BorderLayout.CENTER);
				panel3.setLayout(new GridLayout(13, 17, 0, 0));
				
				label = new JLabel[18];		
				
				//위 숫자 나열
				for (int i = 1; i < 18; i++) {
					if (i == 5 || i == 13) {	//비울칸을 지정해주고 png 파일을 넣어줌
						label[i] = new JLabel();
						panel3.add(label[i]);
						label[i].setIcon(new ImageIcon(S_MovieSeatDialog.class.getResource("/images/blank.png")));
					} else {
						label[i] = new JLabel();
						panel3.add(label[i]);
						label[i].setIcon(new ImageIcon(S_MovieSeatDialog.class.getResource("/images/seat/" + i + ".png")));
					}
				}
				
				seats = new JCheckBox['M'][18];
				//체크박스 나열
				for (int c = 'A'; c < 'M'; c++) {	//열처리
					for (int i = 1; i < 18; i++) {	//행처리
						if (i == 5 || i == 13) {	//비울칸 지정및 png 파일 넣어줌
							seats[c][i] = new JCheckBox();
							panel3.add(seats[c][i]);
							seats[c][i].setIcon(new ImageIcon(S_MovieSeatDialog.class.getResource("/images/blank.png")));

						} else {							
							seats[c][i] = new JCheckBox();	//비울칸 제외한 곳에 체크박스를 넣어줌
							panel3.add(seats[c][i]);							
						}
					}
				}
			}
			
			{
				JPanel buttonPane = new JPanel();
				buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
				getContentPane().add(buttonPane, BorderLayout.SOUTH);
				{
					JButton okButton = new JButton("예매하기");
					okButton.addActionListener(al);						
					okButton.setActionCommand("OK");
					buttonPane.add(okButton);
					getRootPane().setDefaultButton(okButton);					

					List<String> seatinfo =  bd.selectSeat(557);//여기부터 DB 값을 체크박스로 바꿔주기
					
					int cnt = 0;
					for (char c = 'A'; c < 'M'; c++) {
						for (int i = 1; i < 18; ++i) {
							if (i != 5 && i != 13) {
								if (seatinfo.get(cnt).equals("X")) {
									seats[c][i].setSelected(true);
									seats[c][i].setEnabled(false);
									as += 1;
								}
								cnt++;
								
							}
						}//안 for
					}//바깥 for
					//여기까지		
					
				}			
				{
					JButton cancelButton = new JButton("취 소");
					cancelButton.addActionListener(al);
					cancelButton.setActionCommand("Cancel");
					buttonPane.add(cancelButton);
				}
			}
		}
		
	}
	private class BtnActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			switch (e.getActionCommand()) {
			case "OK" :
				
				for (char c = 'A'; c < 'M'; c++) {
					for (int i = 1; i < 18; ++i) {
						if (i != 5 && i != 13 && seats[c][i].isSelected() ) {	
							isSelec = 'X';
							seats[c][i].setEnabled(false);
							if (i > 5 && i < 13) {
								updateSeatSQL = "update seatinfo set " + c + (i-1) + "='" + isSelec + "' where showno = 557";											
								bd.updateSeat(updateSeatSQL);
								
							} else if ( i > 13 && i < 18) {
								updateSeatSQL = "update seatinfo set " + c + (i-2) + "='" + isSelec + "' where showno = 557";											
								bd.updateSeat(updateSeatSQL);
								
							} else {
								updateSeatSQL = "update seatinfo set " + c + i + "='" + isSelec + "' where showno = 557";
								bd.updateSeat(updateSeatSQL);
								
							}
						}
					}//안 for
				}//바깥 for ㄲ
				break;
			case "Cancel" :
				setVisible(false);
				dispose();
				break;
			}
			
		}
		
	}
}


